/*
 * box_selection.cpp
 *
 *  Created on: Oct 14, 2013
 *      Author: filipe
 */
#include<cstdio>
#include<vector>
#include<string>
#include<opencv/cv.h>
#include<opencv/highgui.h>
#include "LandmarkBoxSelector.h"


using namespace std;


vector<string>
read_file(char *filename)
{
	FILE *f;
	char line[1024];
	vector<string> lines;

	if ((f = fopen(filename, "r")) == NULL)
		exit(printf("Arquivo '%s' nao encontrado\n", filename));

	while (!feof(f))
	{
		fscanf(f, "\n%[^\n]\n", line);
		lines.push_back(string(line));
	}

	return lines;
}


void
check_args(int argc, char **argv)
{
	if (argc < 4)
	{
		printf("\n");
		printf("Use %s <arquivo_imagens_referencia> <arquivo_imagens_disparidade> <arquivo_box_selecionados>\n", argv[0]);
		printf("\n");

		exit(-1);
	}
}


int
main(int argc, char **argv)
{
	FILE *f;
	vector<string> image_files;
	vector<string> disparities_image_files;

	check_args(argc, argv);
	image_files = read_file(argv[1]);
	disparities_image_files = read_file(argv[2]);

	f = fopen(argv[3], "w");

	for (size_t i = 50; i < image_files.size(); i++)
	{
		LandmarkBoxSelector box(image_files[i], disparities_image_files[i]);
		box.Loop();

		if (box.BoxSelected())
		{
			fprintf(f, "%s\t%d\t%d\t%d\t%d\t%lf\n",
				image_files[i].c_str(),
				box.GetBox().x,
				box.GetBox().y,
				box.GetBox().x + box.GetBox().width,
				box.GetBox().y + box.GetBox().height,
				box.GetDisparity()
			);
		}

		fflush(f);
	}

	fclose(f);
	return 1;
}



